<?php
class Book
{
	/**
	 * get list 
	 */
	static function getList($s=0,$l=10)
	{
		return Db::get(__CLASS__)->select()->where(array('trip'=>0))->order('book_id DESC')->limit($s,$l)->getAll();
	}
	
	static function getBook($book_id)
	{
		return Db::get(__CLASS__)->select()->where(array('book_id'=>$book_id))->getOne();
	}
	
	/**
	 * 存储并返回书籍存储地址
	 */
	static function storeBook($content, $path=null)
	{
		if(!$path)
		{
			$path	=	BOOK_PATH . date('/y/md/');
			if(!file_exists($path))
			{
				mkdir($path, 0755, true);
			}
		}
		if(!is_writable($path))
		{
			Show::error("目录不可写[$path]");
		}
		$path	.=	(uniqid().'.txt');
		file_put_contents($path, $content);
		return substr($path, strlen(BOOK_PATH));
	}
	
	/**
	 * save the book
	 */
	static function addBook($data)
	{
		Db::get(__CLASS__)->insert($data)->exec();
		return Db::get(__CLASS__)->insertId();
	}
	
	/**
	 * update book info
	 */
	static function editBook($bookid, $data)
	{
		Db::get(__CLASS__)->update($data)->where(array('book_id'=>$bookid))->exec();
		return Db::get(__CLASS__)->affectedRows();
	}
	
	
}
?>